//
// Created by 李望轩 on 2020/11/17.
//
/*
 * 借书人增删改查操作
 */

#include "stdio.h"
#include "stdlib.h"
#include "string.h"


//从文件读入所有学生信息
LeStuLink LeGetStuLinkFroFile(){
    LeStuLink Head, StuLinkList, t;
    Head = (LeStuLink) malloc(sizeof(StuStruct));//生成代插入节点
    Head->Next = NULL;
    t = Head;
    cJSON *root = NULL;//跟节点
    cJSON *Pitem1, *Pitem2;//临时变量
    cJSON *StuJsonList = LeReadJsonList(1, root);//获取Stu数组列表
    int StuJsonListSize = cJSON_GetArraySize(StuJsonList);//获取数组长度
    //文件内容读出到结构体
    for (int i = 0; i < StuJsonListSize; i++) {
        StuLinkList = (LeStuLink) malloc(sizeof(StuStruct));//生成代插入节点
        Pitem1 = cJSON_GetArrayItem(StuJsonList, i);//根据下标读取内容

        Pitem2 = cJSON_GetObjectItem(Pitem1, "SID");
        StuLinkList->SID = Pitem2->valueint;

        Pitem2 = cJSON_GetObjectItem(Pitem1, "SName");
        strcpy(StuLinkList->SName, Pitem2->valuestring);

        Pitem2 = cJSON_GetObjectItem(Pitem1, "SQuota");
        StuLinkList->SQuota = Pitem2->valueint;

        Pitem2 = cJSON_GetObjectItem(Pitem1, "SState");
        StuLinkList->SState = Pitem2->valueint;

        t->Next = StuLinkList;
        t = StuLinkList;
        StuLinkList = NULL;
    }
    t->Next = NULL;
    cJSON_Delete(root);
    return Head;
}

//获取当前StuID最大值
int LeGetMaxStuID(LeStuLink Head) {
    LeStuLink p = Head;
    int i = 99990000;
    while (p->Next) {
        p = p->Next;
        if (p->SID > i) {
            i = p->SID;
        }
    }
    return i;
}

//获取借书人状态，名称，可用余额
int LeGetStuNameAndQuotaAndState(LeStuLink SHead, int SID, char *SName, int *SQuota, int *SState){
    LeStuLink SupP = SHead->Next;
    while (SupP){
        if (SID == SupP->SID){
            *SState = SupP->SState;
            *SQuota = SupP->SQuota;
            strcpy(SName, SupP->SName);
            return 1;
        }
        SupP = SupP->Next;
    }
    return -1;
}

//插入新借书人
void LeInsertStu(LeBookLink BookHead, LeStuLink StuHead, int SID, char *SName, int SQuota, int SState) {

    //进行录入，并按照BID进行排序
    LeStuLink pre = StuHead, p;
    p = (LeStuLink) malloc(sizeof(StuStruct));//生成代插入节点
    p->SID = SID;
    p->SQuota = SQuota;
    p->SState = SState;
    p->Next = NULL;
    strcpy(p->SName, SName);

    //来到最后节点
    while (pre->Next) {
        pre = pre->Next;
    }
    pre->Next = p;
    p = pre = NULL;
    JsonWritFile(BookHead->Next, StuHead->Next);
}

//增加用户额度
void LeAddStuQuota(LeBookLink BHead, LeStuLink SHead, int SID, int AddN){
    LeStuLink Sp = SHead->Next;
    while (Sp){
        if (SID == Sp->SID){
            switch (Sp->SState) {
                case 1:
                    Sp->SQuota += AddN;
                    printf("增加完成\t额度为：%d\n", Sp->SQuota);
                    JsonWritFile(BHead->Next, SHead->Next);
                    return;
                case 0:
                    printf("用户已经挂失，请取消挂失后再试\n");
                    return;
                case -1:
                    printf("用户已经注销！\n");
                    return;
                default:
                    printf("用户状态错误！\n");
                    return;
            }
        }
        Sp = Sp->Next;
    }
    printf("未找到该用户!请重试!\n");
}

//减少用户额度
void LeSubStuQuota(LeBookLink BHead, LeStuLink SHead, int SID, int SubN){
    LeStuLink Sp = SHead->Next;
    while (Sp){
        if (SID == Sp->SID){
            switch (Sp->SState) {
                case 1:

                    Sp->SQuota -= SubN;
                    if (Sp->SQuota <= 0)Sp->SQuota = 0;
                    printf("减少完成\t额度为：%d\n", Sp->SQuota);
                    JsonWritFile(BHead->Next, SHead->Next);
                    return;
                case 0:
                    printf("用户已经挂失，请取消挂失后再试\n");
                    return;
                case -1:
                    printf("用户已经注销！\n");
                    return;
                default:
                    printf("用户状态错误！\n");
                    return;
            }
        }
        Sp = Sp->Next;
    }
    printf("未找到该用户!请重试!\n");
}

//用户挂失，取消挂失，注销
void LeSetStuState(LeBookLink BHead, LeStuLink SHead, int SID, int Flag){
    /*
     * 借书人可用状态 1.可用 0.挂失 -1.注销
     */
    LeStuLink Sp = SHead->Next;
    while (Sp){
        if (SID == Sp->SID){
            switch (Flag) {
                case 1:
                    Sp->SState = 1;
                    JsonWritFile(BHead->Next, SHead->Next);
                    printf("取消挂失/注销完成\n");
                    return;
                case 0:
                    Sp->SState = 0;
                    JsonWritFile(BHead->Next, SHead->Next);
                    printf("用户已挂失完成\n");
                    return;
                case -1:
                    Sp->SState = -1;
                    JsonWritFile(BHead->Next, SHead->Next);
                    printf("用户已注销！\n");
                    return;
                default:
                    printf("状态错误！\n");
                    return;
            }
        }
        Sp = Sp->Next;
    }
    printf("未找到该用户!请重试!\n");
}

//用户完全删除
void LeDelStu(LeBookLink BHead, LeStuLink SHead, int SID){
    /*
     *
     */
    LeStuLink Sp = SHead->Next, Spre = SHead;
    while (Sp){
        if (SID == Sp->SID){

            Spre->Next = Sp->Next;
            free(Sp);
            JsonWritFile(BHead->Next, SHead->Next);
            printf("完全删除完成\n");
            return;
        }
        Spre = Sp;
        Sp = Sp->Next;
    }
    printf("未找到该用户!请重试!\n");
}